<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<el-form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="100px"
				class="demo-ruleForm">
				<el-form-item label="证书全称" prop="certificateName">
					<el-input v-model="ruleForm.certificateName" placeholder="请输入证书全称"></el-input>
				</el-form-item>
				<el-form-item label="颁发时间" prop="issuanceTime">
					<el-date-picker type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"
						placeholder="请选择掌握时间" v-model="ruleForm.issuanceTime" style="width: 100%;">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="颁发单位" prop="issuanceUnit">
					<el-input v-model="ruleForm.issuanceUnit" placeholder="请输入颁发单位"></el-input>
				</el-form-item>
				<el-form-item label="证书描述" prop="name">
					<el-input type="textarea" v-model="ruleForm.desc" rows="4" placeholder="请输入证书描述"></el-input>
				</el-form-item>
				<el-form-item label="证书种类" prop="certificateType">
					<el-select clearable v-model="ruleForm.certificateType" placeholder="请选择证书种类" style="width: 350px;">
						<el-option v-for="(item,index) in certificateOptions" :label="item.dictLabel" :value="item.dictValue"
				 		v-if="item.dictValue != '-1'">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="资格证书附件" prop="">
					<UploadImageList :data="upLoadImages" @uploadImageClick="uploadImageClick_" />
					<!-- <view class="flex aic jcsb" style="line-height: 20px;">
						<el-button class="blue_btn" style="" type="primary">选择上传附件</el-button>
						<view class="blue_btn_p" style="width: calc(100% - 135px);">
							<text class="gray fonts12 ">请上传1寸照，文件大小不超过2M，支持jpg、jpeg、png格式</text>
						</view>
					</view> -->
				</el-form-item>
			</el-form>
		</view>
		<view class="position_footer flex aic jcsb">
			<el-button class="blue_btn" style="width: 100%;" type="primary" @click="submitForm('ruleForm')">保存
			</el-button>
			<el-button class="" style="width: 100%;" type="danger" @click="deleteFun()" v-if="type == 0">删除
			</el-button>
		</view>
	</view>
</template>

<script>
	import {
		editResume
	} from "@/api/my.js"
	import UploadImageList from "@/pages/components/UploadImageList.vue"
	export default {
		components: {
			UploadImageList
		},
		data() {
			return {
				upLoadImages: {
					tip: "（最多上传10张，单张图片大小不超过2M，支持jpg、jpeg、png格式）",
					urls: "",
					limit: 10
				},
				ruleForm: {},
				rules: {
					certificateName: [{
						required: true,
						message: '请输入证书名称',
						trigger: 'blur'
					}],
					issuanceTime: [{
						required: true,
						message: '请输入证书颁发时间',
						trigger: 'blur'
					}],
					issuanceUnit: [{
						required: true,
						message: '请输入证书颁发单位',
						trigger: 'blur'
					}],
					certificateDescription: [{
						required: true,
						message: '请输入证书描述',
						trigger: 'blur'
					}],
					certificateType: [{
						required: true,
						message: '请选择证书种类',
						trigger: 'blur'
					}],

				},
				indexValue: "",
				type: 0,
				certificateOptions:[]
			};
		},
		onLoad(options) {
			
			this.datas = JSON.parse(options.data)
			this.type = options.type
			if (options.type == 0) { //修改
				this.indexValue = options.index
				this.info(this.datas, options.index)
			} else { //新增	
			}

		},
		methods: {
			info(data, index) {
				this.certificateOptions = uni.getStorageSync("sys_certificate_type")
				console.log("资格证书",this.certificateOptions);
				console.log("data:", data);
				this.ruleForm = data.certificates[index]
				this.upLoadImages.urls = data.certificates[index].files
			},
			uploadImageClick_(value) {
				console.log("多图片：", value);
				this.ruleForm.ossIds = value.ossIds;
				this.ruleForm.files = value.urls
			},
			deleteFun(){
				let obj = this.datas
				obj.certificates.splice(this.indexValue,1)
				editResume(obj).then(res => {
					uni.showToast({
						title: "已删除",
						icon: 'error'
					})
					if (this.type == 1) {
						uni.navigateBack("-1")
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				})
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						let obj = this.datas
						console.log("工作经历数据：", obj);
						if (this.type == 0) {
							obj.certificates[this.indexValue] = this.ruleForm
						} else {
							obj.certificates.push(this.ruleForm)
						}

						editResume(obj)
					} else {
						this.$message({
							message: '请完善信息',
							type: 'error',
							offset: 0.1
						});
						console.log('error submit!!');
						return false;
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
